#####################################################################
# Product:      KnowledgeEngine (Plone Edition)
#
# File:         ToolbarTool.py
#
# Copyright:    Copyright (c) 2004, Faulkner Technologies
#
# Author:       Brian Oliver
#
# Description:  This file defines the ToolbarTool class, the base 
#               class for simple Elements for placement in Toolbars
#
# See Also:     Element, Toolbar
#####################################################################

#####################################################################
## Knowledge Engine Library Imports
from Products.KnowledgeEngine.Common import *
from Products.KnowledgeEngine.Core import *
from Products.KnowledgeEngine.Element import Element, ElementInfo
from Products.KnowledgeEngine.PropertyDefinition import *
from Products.KnowledgeEngine.PropertyFormats import *
from Products.KnowledgeEngine.PropertyTypes import *

#####################################################################
## Zope Library Imports
from Globals import DTMLFile
from App.ImageFile import ImageFile

#####################################################################
## Python Library Imports
# (none)

#####################################################################
## ToolbarTool Class Definition
class ToolbarToolInfo(ElementInfo):
    """
        Defines the class info methods, defining information about
        the class without needing an instance of the class
    """
    
    #---------------------------------------------------------------#
    
    def getClassName(self):
        """
            Returns the name of the class for which 
            we are providing information.
        """
        return "ToolbarTool"

    #---------------------------------------------------------------#

    def getDisplayName(self):
        """
            Return a friendly name for the class instances
            (this is used for displaying in lists etc)
        """
    
        return "ToolbarTool"
        
    #---------------------------------------------------------------#

    def getIcon(self, relative_to_portal=0):
        """
            PLONE Support:
            
            Returns a URL to the icon for the object
        """
                
        #tools like png format (for some reason)                
        return self.getClassName() + '_icon.png'
        
    #---------------------------------------------------------------#

    def getIdentityType(self):
        """
            Return the identity type.  
        """
        
        return "tool"
        
    #---------------------------------------------------------------#

    def isPermittedInContainer(self, containerClassInfo, containerInstance = None, instance=None):
        """
            ABSTRACT: Returns if class instances (instance) is permitted in 
            the specified container (containerInstance)
            
            NOTE: This is from this class' instance perspective.
        """
        
        #DEFAULT RULE: ToolbarTools are only permitted in Toolbars
        return containerClassInfo.isInstanceOf('Toolbar')

    #---------------------------------------------------------------#

    def isInstanceOf(self, className):
        """
            Returns if the class is an instance of the specified
            className, or implements the interface specified 
            by the className
        """

        return className == 'ToolbarTool' or ElementInfo.isInstanceOf(self, className)
    
    #---------------------------------------------------------------#

    def onConfigureProperties(self):
        """
            Automatically called by the Knowledge Engine once
            the class has been loaded.  Use this method to register 
            class specific properties using the PropertyReflection interface.
            
            If this class is sub-classed, a call must be made to
            the super-class to ensure super-class properties are
            correctly registered
            
            NOTE: This method is called before ClassInfo.onAfterStartup()            
        """

        #initialise the super-class properties for reflection
        ElementInfo.onConfigureProperties(self)
      
        #add the 'order' property        
        self.addPropertyDefinition(PropertyDefinition(identity="order", 
                                                      displayName="Order", 
                                                      description="The position of the tool in the toolbar (decimals permitted)", 
                                                      propertyType=pt_FLOAT, 
                                                      defaultValue=1.0, 
                                                      constructorEVAP=evap_NONE, 
                                                      propertyEditorEVAP=evap_NONE))

        #add the 'alignment' property        
        self.addPropertyDefinition(PropertyDefinition(identity="alignment", 
                                                      displayName="Alignment", 
                                                      description="The alignment of the tool in the toolbar", 
                                                      propertyType=pt_STRING, 
                                                      propertyFormat=pf_NATIVE, 
                                                      hasEnumeratedValues=true, 
                                                      enumeratedValuesList=['Left','Center','Right'], 
                                                      defaultValue="Left"))
        
    #---------------------------------------------------------------#

#####################################################################
## ToolbarTool Class Definition
class ToolbarTool(Element):
    """
        A class to represent an element in a toolbar
    """

    #---------------------------------------------------------------#

    #ABSTRACT TRANSIENT: a ClassInfo implementation instance to provide info about this class
    _CLASSINFO = ToolbarToolInfo()

    #---------------------------------------------------------------#

    # Custom zope manage_preview to permit previews of components.
    preview_html = DTMLFile('dtml/index_html', globals())

    #---------------------------------------------------------------#

    ALIGNMENT_TYPES = ['Left','Center','Right']

    #---------------------------------------------------------------#

    def __init__(self, id, identity):
        "Constructor for the Instance"        

        Element.__init__(self, id, identity)
        
        self.order = 1.0
        self.alignment = 'Left'
        
        #perform onAfterInstanciation processing
        self.onAfterInstanciation()

    #---------------------------------------------------------------#

    def onAfterInstanciation(self):
        """
            Handle Knowledge Engine specific actions after this object 
            has been loaded into memory.  
            
            This is useful for performing backwards compatibility
        """        
        
        #perform default processing
        Element.onAfterInstanciation(self)
        
    #---------------------------------------------------------------#

    def getAlignment(self):
        """
            Return the alignment of this element in the toolbar
        """        

        return self.alignment

    #---------------------------------------------------------------#

    def setAlignment(self, alignment):
        """
            Set the alignment of this element in the toolbar
        """        

        self.alignment = alignment
        
    #---------------------------------------------------------------#

    def hasOwnOrder(self):
        """
            Returns if the Element defines it's own order and should not
            be destroyed by updateDisplayPositionInModels method
        """
        
        #toolbar elements have their own order
        return true
        
    #---------------------------------------------------------------#

#####################################################################
## Class Initialisation

#####################################################################
